package com.yujing.yview

import android.app.Activity
import android.text.InputFilter
import androidx.core.content.ContextCompat
import com.yujing.yview.base.YBaseDialog
import com.yujing.yview.databinding.DialogInputBinding

/**
 * 输入框
 * @author yujing 2021年9月7日10:29:12
 */
/*
用法：
InputDialog(this).apply {
    textSizeScale = 2.0F
    maxLength = 10 //最大长度
    setListener { text ->
        textView1.text = "内容：$text"
    }
    tvHide = "请输入内容"
    show()
}
 */
class InputDialog(activity: Activity) : YBaseDialog<DialogInputBinding>(activity, R.layout.dialog_input) {
    init {
        fullscreen = true
        widthPixels = 0.4F
        heightPixels = 0.6F
        fillColor = ContextCompat.getColor(activity, R.color.fill_background)
    }

    private var listener: ((String?) -> Unit)? = null
    var btNameOK = "确　定"
    var btNameCancel = "取　消"
    var tvTitle = "请输入内容"
    var tvHide = "请输入"
    var tvTips = ""
    var default: String? = null
    var maxLength = Int.MAX_VALUE //最大输入长度

    //文字大小比例
    var textSizeScale: Float = 1.0F

    //初始化完成
    var initCompleteListener: ((DialogInputBinding) -> Unit)? = null


    //确定事件
    private fun confirm() {
        val api: String = binding.etInput.text.toString()
        listener?.invoke(api)
    }

    override fun init() {
        //设置默认值
        if (default != null)
            binding.etInput.setText(default)
        binding.etInput.setSelection(binding.etInput.text.toString().length)
        binding.til.hint = tvHide
//        binding.etInput.hint = tvHide
        binding.btConfirm.text = btNameOK
        binding.btCancel.text = btNameCancel
        binding.tvTitle.text = tvTitle
        binding.tvTips.text = tvTips

        //确定按钮监听
        binding.btConfirm.setOnClickListener {
            confirm()
        }
        //取消按钮监听
        binding.btCancel.setOnClickListener { dismiss() }
        //ip获取焦点
        view.post { binding.etInput.requestFocus() }
        //设置最大输入长度
        binding.etInput.filters = arrayOf<InputFilter>(
            InputFilter.LengthFilter(maxLength)
        )

        //---------------------------缩放---------------------------
        binding.tvTitle.also { it.textSize = px2sp(it.textSize.toInt()) * textSizeScale }
        binding.etInput.also { it.textSize = px2sp(it.textSize.toInt()) * textSizeScale }
        binding.tvTips.also { it.textSize = px2sp(it.textSize.toInt()) * textSizeScale }

        //输入框高度缩放
        binding.etInput.also {
            val lp = it.layoutParams
            lp.height = (lp.height * textSizeScale).toInt()
            it.layoutParams = lp
        }
        //按钮高度缩放
        binding.llBt.also {
            val lp = it.layoutParams
            lp.height = (lp.height * textSizeScale).toInt()
            it.layoutParams = lp
        }
        //设置按钮文字大小
        binding.btCancel.also { it.textSize = px2sp(it.textSize.toInt()) * textSizeScale }
        binding.btConfirm.also { it.textSize = px2sp(it.textSize.toInt()) * textSizeScale }
        //---------------------------缩放---------------------------
        initCompleteListener?.invoke(binding)
    }

    fun setListener(listener: ((String?) -> Unit)?) {
        this.listener = listener
    }
}
